package com.ruoyi.web.controller.admin.msg;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.dto.MsgTemplatePreviewDTO;
import com.ruoyi.system.entity.MsgTemplateDO;
import com.ruoyi.system.service.MsgTemplateService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/admin/msg/template")
public class MsgTemplateController extends BaseController {

    @Resource
    private MsgTemplateService templateService;

    @GetMapping("/list")
    public AjaxResult list(@RequestParam(required = false) Integer type,
                           @RequestParam(required = false) Integer status) {
        List<MsgTemplateDO> list = templateService.listByTypeAndStatus(type, status);
        return AjaxResult.success(list);
    }

    @PostMapping("/create")
    public AjaxResult create(@RequestBody MsgTemplateDO dto) {
        return AjaxResult.success(templateService.createTemplate(dto));
    }

    @PutMapping("/update")
    public AjaxResult update(@RequestBody MsgTemplateDO dto) {
        return AjaxResult.success(templateService.updateTemplate(dto));
    }

    @PutMapping("/{id}/status")
    public AjaxResult status(@PathVariable Long id, @RequestParam Integer status) {
        return AjaxResult.success(templateService.changeStatus(id, status));
    }

    @PostMapping("/{id}/copy")
    public AjaxResult copy(@PathVariable Long id, @RequestParam String newName) {
        return AjaxResult.success(templateService.copyTemplate(id, newName));
    }

    @PostMapping("/preview")
    public AjaxResult preview(@Valid @RequestBody MsgTemplatePreviewDTO previewDTO) {
        return AjaxResult.success(templateService.previewContent(previewDTO));
    }

    /**
     * 删除消息模板
     * 
     * @param id 模板ID
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable Long id) {
        boolean result = templateService.deleteTemplate(id);
        return result ? AjaxResult.success("删除成功") : AjaxResult.error("删除失败");
    }

    /**
     * 批量删除消息模板
     * 
     * @param ids 模板ID列表
     * @return 删除结果
     */
    @DeleteMapping("/batch")
    public AjaxResult deleteBatch(@RequestBody List<Long> ids) {
        boolean result = templateService.deleteTemplates(ids);
        return result ? AjaxResult.success("批量删除成功") : AjaxResult.error("批量删除失败");
    }
}
